{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第10章.时间序列数据"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.1.时间戳\n",
    "<p>时间戳是很常用的事件类型,本节内容主要有两部分组成：第一部分是时间戳与时间戳序列，第二部分是dt对象.</p>\n",
    "<p>其中第一部分将从构造方式,属性以及切片索引3个角度做解析</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.1.1.时间戳的构造与属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-01 00:00:00')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 单个时间戳的生成可以利用pd.TimeStamp()实现\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "ts=pd.Timestamp('2021/9/1') # 生成单个时间戳\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-01 08:30:10')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts=pd.Timestamp('2021-9-1 08:30:10')\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-12 00:00:00')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts=pd.Timestamp('20210912')\n",
    "ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021年9月12日0时0分0秒'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间戳的时，分，秒，年，月，日等数据可以通过hour,minute,second,year,month,day属性获取\n",
    "f\"{ts.year}年{ts.month}月{ts.day}日{ts.hour}时{ts.minute}分{ts.second}秒\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.1.2时间序列的生成\n",
    "<p>时间戳序列可以使用to_datetime()方法生成，该方法能够把一系列时间戳转化为datetime64[ns]类型的时间戳序列</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-01', '2021-01-02', '2021-01-03'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(['2021-1-1','2021-1-2','2021-1-3'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "      <th>Test_Number</th>\n",
       "      <th>Test_Date</th>\n",
       "      <th>Time_Record</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Gaopeng Yang</td>\n",
       "      <td>Female</td>\n",
       "      <td>158.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-10-05</td>\n",
       "      <td>0:04:34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Changqiang You</td>\n",
       "      <td>Male</td>\n",
       "      <td>166.5</td>\n",
       "      <td>70.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019-09-04</td>\n",
       "      <td>0:04:20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Mei Sun</td>\n",
       "      <td>Male</td>\n",
       "      <td>188.9</td>\n",
       "      <td>89.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019-09-12</td>\n",
       "      <td>0:05:22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>C</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>NaN</td>\n",
       "      <td>41.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2020-01-03</td>\n",
       "      <td>0:04:08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>C</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Gaojuan You</td>\n",
       "      <td>Male</td>\n",
       "      <td>174.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019-11-06</td>\n",
       "      <td>0:05:22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  School      Grade            Name  Gender  Height  Weight Transfer  \\\n",
       "0      A   Freshman    Gaopeng Yang  Female   158.9    46.0        N   \n",
       "1      B   Freshman  Changqiang You    Male   166.5    70.0        N   \n",
       "2      A     Senior         Mei Sun    Male   188.9    89.0        N   \n",
       "3      C  Sophomore    Xiaojuan Sun  Female     NaN    41.0        N   \n",
       "4      C  Sophomore     Gaojuan You    Male   174.0    74.0        N   \n",
       "\n",
       "   Test_Number  Test_Date Time_Record  \n",
       "0            1 2019-10-05     0:04:34  \n",
       "1            1 2019-09-04     0:04:20  \n",
       "2            2 2019-09-12     0:05:22  \n",
       "3            2 2020-01-03     0:04:08  \n",
       "4            2 2019-11-06     0:05:22  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从文件中读取数据时\n",
    "# 可直接通过parse_dates将指定的某一列或多列解析为datetime64[ns]类型\n",
    "df=pd.read_csv(\"F:\\python学习资料\\pandas配套数据集+习题答案及源代码\\配套源代码+习题答案及源代码\\data\\learn_pandas.csv\"\n",
    "               ,parse_dates=['Test_Date'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2019-10-05\n",
       "1   2019-09-04\n",
       "2   2019-09-12\n",
       "3   2020-01-03\n",
       "4   2019-11-06\n",
       "Name: Test_Date, dtype: datetime64[ns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=df.Test_Date\n",
    "s.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-01 08:30:15', '2021-01-03 09:30:55'], dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果时间戳的格式不满足条件，无法转化\n",
    "# 则可以使用format强行匹配,字母Y,m,d,H,M,S分别表示年，月，日，时，分，秒\n",
    "bad_format=['2021\\\\1\\\\1 8 30 15','2021\\\\1\\\\3 9 30 55']\n",
    "temp=pd.to_datetime(bad_format,\n",
    "              format='%Y\\\\%m\\\\%d %H %M %S'\n",
    "              )\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-01-01 08:30:15\n",
       "1   2021-01-03 09:30:55\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意，转化为Seris类型\n",
    "pd.Series(temp).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练一练:将如下格式的时间戳序列转为正确格式的时间序列Series\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-09-01\n",
       "1   2021-09-02\n",
       "2   2021-09-03\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ['0901 2021','0902,2021','0903 2021']\n",
    "temp=pd.to_datetime(['0901 2021','0902 2021','0903 2021'],format=\"%m%d %Y\")\n",
    "temp=pd.Series(temp)\n",
    "temp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-09-01 08:35:50\n",
       "1   2021-09-02 09:25:45\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ['2021-9-1 8-35-50','2021-9-2 9-25-45']\n",
    "temp=pd.to_datetime(['2021-9-1 8-35-50','2021-9-2 9-25-45'],format='%Y-%m-%d %H-%M-%S')\n",
    "pd.Series(temp)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 此外to_datetime()还可以将多列的时间属性拼接并将其转化为时间序列\n",
    "#### 此时的列名必须和规定的时间关键字列名(year,month,day,hour,minute,second)一致"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th>day</th>\n",
       "      <th>hour</th>\n",
       "      <th>minute</th>\n",
       "      <th>second</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2021</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>30</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2021</td>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>50</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   year  month  day  hour  minute  second\n",
       "0  2021      9    1    10      30      20\n",
       "1  2021      9    2    20      50      40"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_date_cols=pd.DataFrame({\n",
    "    'year':[2021,2021],\n",
    "    'month':[9,9],\n",
    "    'day':[1,2],\n",
    "    'hour':[10,20],\n",
    "    'minute':[30,50],\n",
    "    'second':[20,40]\n",
    "})\n",
    "df_date_cols"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-09-01 10:30:20\n",
       "1   2021-09-02 20:50:40\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_datetime(df_date_cols)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### date_range()函数\n",
    "<P>date_range函数是一种生成连续间隔时间的函数，其重要的参数有start,end,freq,periods.</P>\n",
    "<P>他们分别表示开始时间,结束时间,时间间隔,时间戳个数</P>\n",
    "<p>其中,四个参数中的三个参数确定后,那么剩下的一个参数也就确定了</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-09-01', '2021-09-11', '2021-09-21'], dtype='datetime64[ns]', freq='10D')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2021-9-1','2021-9-21',freq='10D') # 间隔10天"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-09-01', '2021-09-11', '2021-09-21', '2021-10-01',\n",
       "               '2021-10-11', '2021-10-21'],\n",
       "              dtype='datetime64[ns]', freq='10D')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('2021-9-1','2021-10-28',freq='10D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-09-01 00:00:00', '2021-09-12 09:36:00',\n",
       "               '2021-09-23 19:12:00', '2021-10-05 04:48:00',\n",
       "               '2021-10-16 14:24:00', '2021-10-28 00:00:00'],\n",
       "              dtype='datetime64[ns]', freq=None)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 当未给出freq时,终止日期会被视为断点\n",
    "pd.date_range('2021-9-1','2021-10-28',periods=6)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### asfreq()函数.该函数可以改变序列采样频率\n",
    "<P>该函数有两个重要参数：freq表示频率,method表示填充方法</P>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-09-01    1.0\n",
       "2021-09-08    2.0\n",
       "2021-09-10    NaN\n",
       "2021-09-15    4.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series(\n",
    "    [1,2,np.nan,4],\n",
    "    index=pd.to_datetime(['20210901','20210908','20210910','20210915'])\n",
    ")\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-09-01    1.0\n",
       "2021-09-04    NaN\n",
       "2021-09-07    NaN\n",
       "2021-09-10    NaN\n",
       "2021-09-13    NaN\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.asfreq('3D')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-09-01    1.0\n",
       "2021-09-04    1.0\n",
       "2021-09-07    1.0\n",
       "2021-09-10    NaN\n",
       "2021-09-13    NaN\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.asfreq('3D',method='ffill')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-09-01    1.0\n",
       "2021-09-04    2.0\n",
       "2021-09-07    2.0\n",
       "2021-09-10    NaN\n",
       "2021-09-13    4.0\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.asfreq('3D',method='bfill')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.1.3 dt对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<p>pandas在时间序列上定义了dt对象。已完成许多时间序列的相关操作</p>\n",
    "<p>对于datetime64[ns]类型而言,对时间序列的操作可分为大致三类</p>\n",
    "<ul>\n",
    "    <li>取出与时间相关的属性</li>\n",
    "    <li>判断时间戳是否满足条件</li>\n",
    "    <li>取整操作</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<strong>第一类操作常用的属性有:date,time,month,day,hour,minute，quarter等</strong>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-01-01\n",
       "1   2021-01-02\n",
       "2   2021-01-03\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "s=pd.Series(pd.date_range('2021-1-1','2021-1-3',freq='D'))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    2021-01-01\n",
       "1    2021-01-02\n",
       "2    2021-01-03\n",
       "dtype: object"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取日期\n",
    "s.dt.date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    00:00:00\n",
       "1    00:00:00\n",
       "2    00:00:00\n",
       "dtype: object"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取时分秒\n",
    "s.dt.time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取每月中的天数\n",
    "s.dt.day"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    4\n",
       "1    5\n",
       "2    6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取日期中的星期情况，周一为0，周二为1......\n",
    "s.dt.dayofweek"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    January\n",
       "1    January\n",
       "2    January\n",
       "dtype: object"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#此外可通过month_name()和day_name()属性以英文方式返回月名和星期名\n",
    "s.dt.month_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0      Friday\n",
       "1    Saturday\n",
       "2      Sunday\n",
       "dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.dt.day_name()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<strong>第二类操作主要用于测试时间戳是否为年,季，月的第一天或最后一天</strong>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     True\n",
       "1    False\n",
       "2    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.dt.is_year_start # 还可选is_quarter_start或is_month_start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.dt.is_year_end # 同上"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<strong>第三类操作主要是取整，使用round(),ceil(),floor()实现，他们的公共参数为freq。常用的采样频率包括H(小时),min(分钟),S（秒）</strong>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-01-01 20:35:00\n",
       "1   2021-01-01 21:20:00\n",
       "2   2021-01-01 22:05:00\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s=pd.Series(pd.date_range('2021-1-1 20:35:00','2021-1-1 22:35:00',freq='45min'))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-01-01 21:00:00\n",
       "1   2021-01-01 21:00:00\n",
       "2   2021-01-01 22:00:00\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 四舍五入取整\n",
    "s.dt.round('1H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-01-01 20:00:00\n",
       "1   2021-01-01 21:00:00\n",
       "2   2021-01-01 22:00:00\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向上取整\n",
    "s.dt.floor('1H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   2021-01-01 21:00:00\n",
       "1   2021-01-01 22:00:00\n",
       "2   2021-01-01 23:00:00\n",
       "dtype: datetime64[ns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 向下取整\n",
    "s.dt.ceil(\"1H\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.1.4 时间戳序列的索引和切片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<P>如果需要筛选出某个时间戳的子序列</P>\n",
    "<P>第一类方法是联合使用dt对象和布尔条件.第二类方法是使用切片.后者常用于连续时间戳</P>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01    0\n",
       "2021-01-02    1\n",
       "2021-01-03    1\n",
       "2021-01-04    1\n",
       "2021-01-05    1\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "s=pd.Series(np.random.randint(2,size=365),index=pd.date_range('2021-1-1','2021-12-31'))\n",
    "idx=pd.Series(s.index).dt #获取日期索引的dt对象\n",
    "s.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01    0\n",
       "2021-01-31    0\n",
       "2021-02-01    1\n",
       "2021-02-28    0\n",
       "2021-03-01    1\n",
       "dtype: int32"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 首先演示第一类方法\n",
    "# 取出每月的第一天和最后一天\n",
    "s[(idx.is_month_start|idx.is_month_end).values].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-02    1\n",
       "2021-01-03    1\n",
       "2021-01-09    1\n",
       "2021-01-10    1\n",
       "2021-01-16    0\n",
       "dtype: int32"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出所有的双休日\n",
    "s[idx.dayofweek.isin([5,6]).values].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练一练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01    0\n",
       "2021-01-04    1\n",
       "2021-01-05    1\n",
       "2021-01-06    1\n",
       "2021-01-07    1\n",
       "dtype: int32"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选出每月前10天中的工作日\n",
    "s[((idx.day<=10) & idx.dayofweek.isin([0,1,2,3,4]) ).values].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01    0\n",
       "2021-01-11    1\n",
       "2021-01-21    1\n",
       "2021-01-27    0\n",
       "2021-01-28    0\n",
       "dtype: int32"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 选出每月的1日,11日和21日以及每月的最后5天\n",
    "s[( idx.day.isin([1,11,21])  | ((idx.daysinmonth-idx.day)<5)  ).values].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 接着演示第二类方法\n",
    "s['2021-01-01']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s['20210102']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-07-01    1\n",
       "2021-07-02    0\n",
       "2021-07-03    0\n",
       "2021-07-04    1\n",
       "2021-07-05    0\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模糊匹配\n",
    "s['2021-07'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-05-01    0\n",
       "2021-05-02    1\n",
       "2021-05-03    0\n",
       "2021-05-04    1\n",
       "2021-05-05    1\n",
       "Freq: D, dtype: int32"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s['2021-05':'2021-07'].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.2时间差\n",
    "<P>时间差即两个时刻之差,本节主要讨论时间差对象的生成和计算</P>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.2.1.时间差序列的生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('1 days 00:25:00')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据定义,单个时间差对象能够通过两个时间戳做差得到\n",
    "import pandas as pd\n",
    "pd.Timestamp('2021-01-02 08:00:00')-pd.Timestamp(\"2021-01-01 07:35:00\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('1 days 00:35:00')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成单个时间差也可使用pd.Timedelta()来直接构造\n",
    "pd.Timedelta(days=1,minutes=35) # 制定时间单位参数,注意需要加s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('1 days 00:25:00')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timedelta(\"1 days 25 minutes\") # 字符串生成"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>School</th>\n",
       "      <th>Grade</th>\n",
       "      <th>Name</th>\n",
       "      <th>Gender</th>\n",
       "      <th>Height</th>\n",
       "      <th>Weight</th>\n",
       "      <th>Transfer</th>\n",
       "      <th>Test_Number</th>\n",
       "      <th>Test_Date</th>\n",
       "      <th>Time_Record</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Gaopeng Yang</td>\n",
       "      <td>Female</td>\n",
       "      <td>158.9</td>\n",
       "      <td>46.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019/10/5</td>\n",
       "      <td>0:04:34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>Freshman</td>\n",
       "      <td>Changqiang You</td>\n",
       "      <td>Male</td>\n",
       "      <td>166.5</td>\n",
       "      <td>70.0</td>\n",
       "      <td>N</td>\n",
       "      <td>1</td>\n",
       "      <td>2019/9/4</td>\n",
       "      <td>0:04:20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>A</td>\n",
       "      <td>Senior</td>\n",
       "      <td>Mei Sun</td>\n",
       "      <td>Male</td>\n",
       "      <td>188.9</td>\n",
       "      <td>89.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019/9/12</td>\n",
       "      <td>0:05:22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>C</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Xiaojuan Sun</td>\n",
       "      <td>Female</td>\n",
       "      <td>NaN</td>\n",
       "      <td>41.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2020/1/3</td>\n",
       "      <td>0:04:08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>C</td>\n",
       "      <td>Sophomore</td>\n",
       "      <td>Gaojuan You</td>\n",
       "      <td>Male</td>\n",
       "      <td>174.0</td>\n",
       "      <td>74.0</td>\n",
       "      <td>N</td>\n",
       "      <td>2</td>\n",
       "      <td>2019/11/6</td>\n",
       "      <td>0:05:22</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  School      Grade            Name  Gender  Height  Weight Transfer  \\\n",
       "0      A   Freshman    Gaopeng Yang  Female   158.9    46.0        N   \n",
       "1      B   Freshman  Changqiang You    Male   166.5    70.0        N   \n",
       "2      A     Senior         Mei Sun    Male   188.9    89.0        N   \n",
       "3      C  Sophomore    Xiaojuan Sun  Female     NaN    41.0        N   \n",
       "4      C  Sophomore     Gaojuan You    Male   174.0    74.0        N   \n",
       "\n",
       "   Test_Number  Test_Date Time_Record  \n",
       "0            1  2019/10/5     0:04:34  \n",
       "1            1   2019/9/4     0:04:20  \n",
       "2            2  2019/9/12     0:05:22  \n",
       "3            2   2020/1/3     0:04:08  \n",
       "4            2  2019/11/6     0:05:22  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 准备数据\n",
    "df=pd.read_csv(\"F:\\python学习资料\\pandas配套数据集+习题答案及源代码\\配套源代码+习题答案及源代码\\data\\learn_pandas.csv\")\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   00:04:34\n",
       "1   00:04:20\n",
       "2   00:05:22\n",
       "3   00:04:08\n",
       "4   00:05:22\n",
       "Name: Time_Record, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成时间差序列\n",
    "s=pd.to_timedelta(df.Time_Record)\n",
    "s.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimedeltaIndex(['00:00:00', '00:06:00', '00:12:00'], dtype='timedelta64[ns]', freq='6T')"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 与date_range()一样,时间差序列也可以用timedelta_range()来生成,二者都有freq和periods两个参数\n",
    "pd.timedelta_range('0s','1000s',freq='6min')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimedeltaIndex(['00:00:00', '00:08:20', '00:16:40'], dtype='timedelta64[ns]', freq=None)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.timedelta_range('0s','1000s',periods=3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Timedelta序列同样定义了dt对象\n",
    "<P>dt对象上定义的属性有days，seconds,microseconds,nanoseconds</P>\n",
    "<P>需要注意的是，这里的seconds不是本身的秒数,而是对天数取余后剩余的秒数<br>\n",
    " 具体来说,假设时间差为1分6秒,seconds属性返回的是66s\n",
    "</P>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    274\n",
       "1    260\n",
       "2    322\n",
       "3    248\n",
       "4    322\n",
       "Name: Time_Record, dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.dt.seconds.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    34.0\n",
       "1    20.0\n",
       "2    22.0\n",
       "3     8.0\n",
       "4    22.0\n",
       "Name: Time_Record, dtype: float64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果想直接获得对应秒数,则可先用total_seconds()得到总秒数，之后对60取余\n",
    "(s.dt.total_seconds()%60).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   00:05:00\n",
       "1   00:04:00\n",
       "2   00:05:00\n",
       "3   00:04:00\n",
       "4   00:05:00\n",
       "Name: Time_Record, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 与时间戳序列相似，取整函数也是可以在dt对象上使用\n",
    "s.dt.round('min').head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.2.2.时间差序列的运算\n",
    "<p>时间差对象支持的常用运算有3类:</p>\n",
    "<ul>\n",
    "    <li>与标量的乘法运算</li>\n",
    "    <li>与时间戳的加减法运算</li>\n",
    "    <li>与时间差的加减法和除法运算</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "td1=pd.Timedelta(days=1)\n",
    "td2=pd.Timedelta(days=3)\n",
    "ts=pd.Timestamp('20210101')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('2 days 00:00:00')"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1*2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td2/td1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timedelta('2 days 00:00:00')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td2-td1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-01-02 00:00:00')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts+td1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2020-12-31 00:00:00')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ts-td1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 这些操作都可以移植到时间差序列上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "td1=pd.timedelta_range(start=\"1 days\",periods=5)\n",
    "td2=pd.timedelta_range(start=\"12 hours\",periods=5,freq='2H')\n",
    "ts=pd.date_range('20210101','20210105')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimedeltaIndex(['5 days', '10 days', '15 days', '20 days', '25 days'], dtype='timedelta64[ns]', freq='5D')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1*5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0 days\n",
       "1    2 days\n",
       "2    6 days\n",
       "3   12 days\n",
       "4   20 days\n",
       "dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1*pd.Series(list(range(5))) # 逐个相乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimedeltaIndex(['1 days', '2 days', '3 days', '4 days', '5 days'], dtype='timedelta64[ns]', freq='D')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimedeltaIndex(['12:00:00', '14:00:00', '16:00:00', '18:00:00', '20:00:00'], dtype='timedelta64[ns]', freq='2H')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "TimedeltaIndex(['0 days 12:00:00', '1 days 10:00:00', '2 days 08:00:00',\n",
       "                '3 days 06:00:00', '4 days 04:00:00'],\n",
       "               dtype='timedelta64[ns]', freq='22H')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1-td2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Float64Index([2.0, 3.4285714285714284, 4.5, 5.333333333333333, 6.0], dtype='float64')"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1/td2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-02', '2021-01-03', '2021-01-04', '2021-01-05',\n",
       "               '2021-01-06'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1+pd.Timestamp('20210101')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-02', '2021-01-04', '2021-01-06', '2021-01-08',\n",
       "               '2021-01-10'],\n",
       "              dtype='datetime64[ns]', freq='2D')"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "td1+ts # 逐个相加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0   00:04:34\n",
       "1   00:04:20\n",
       "2   00:05:22\n",
       "3   00:04:08\n",
       "4   00:05:22\n",
       "Name: Time_Record, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练一练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5592417061611374"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 最慢学生花费的秒数是最快学生的多少倍\n",
    "(s.max())/(s.min())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    00:04:34\n",
       "2    00:05:22\n",
       "4    00:05:22\n",
       "8    00:05:13\n",
       "10   00:05:16\n",
       "Name: Time_Record, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_slow=s[s>pd.Timedelta(minutes=4,seconds=30)] # 获取四分30秒以上的同学\n",
    "s_slow.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     00:04:34\n",
       "1     00:05:22\n",
       "2     00:05:22\n",
       "3     00:05:13\n",
       "4     00:05:16\n",
       "        ...   \n",
       "100   00:05:08\n",
       "101   00:04:31\n",
       "102   00:04:48\n",
       "103   00:04:58\n",
       "104   00:05:05\n",
       "Name: Time_Record, Length: 105, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res=s_slow.reset_index(drop=True)\n",
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "00:04:30    15\n",
       "00:04:40    12\n",
       "00:04:50    16\n",
       "00:05:00    18\n",
       "00:05:10    20\n",
       "00:05:20    24\n",
       "Name: Time_Record, dtype: int64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.to_timedelta(res // pd.Timedelta('10s')*10,unit='s').value_counts().sort_index()\n",
    "# 注意 // 为取整运算符,value_counts()可用于统计序列中相同值的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "27"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "279 // 10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.3 日期偏置\n",
    "<p>日期偏置是一种和日历相关的特殊时间差,本节主要介绍Offset对象的使用方法,采样频率与日期偏置的关系</p>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.3.1 Offset对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-06 00:00:00')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 求2021年9月第一个周一的日期\n",
    "import pandas as pd\n",
    "pd.Timestamp('20210831')+pd.offsets.WeekOfMonth(week=0,weekday=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-10-18 00:00:00')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2021年9月6日后的第30个工作日\n",
    "pd.Timestamp('20210906')+pd.offsets.BDay(30)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 使用加号表示获取最近的下一个偏置日期,而用减号表示获取最近的上一个偏置日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-06 00:00:00')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('20211001') - pd.offsets.WeekOfMonth(week=0,weekday=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 练一练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-22 00:00:00')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp(\"20210906\")+pd.offsets.BDay(12)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-21 00:00:00')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('20210905')+pd.offsets.BDay(12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 常用的日期偏置类型及含义\n",
    "|偏置类型(B表示工作日)|含义|\n",
    "|:----:|:----:|\n",
    "| BDay | 工作日 |\n",
    "| Cday | 自定义日 |\n",
    "| Week | 星期或周 |\n",
    "| WeekOfMonth | 每月第week周的第weekday天 |\n",
    "| LastWeekOfMonth | 每月最后的星期weekday |\n",
    "| (B)MonthBrgin / (B)MonthEnd | 月初/月末 |\n",
    "| (B)QuarterBegin / (B)QuarterEnd | 季初/季末  |\n",
    "| (B)YearBegin / (B)YearEnd | 年初/年末 |\n",
    "\n",
    "<P>上述偏置类型都有一个公共参数n,他表示偏置增加的数量</P>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2023-01-01 00:00:00')"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('20210906') + pd.offsets.YearBegin(2) # 两年后的年初"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2023-01-01 00:00:00')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('20210906')+pd.offsets.YearBegin(1)+pd.offsets.YearBegin(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2021-09-29 00:00:00')"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# LastWeekOfmonth\n",
    "pd.Timestamp('20210906')+ pd.offsets.LastWeekOfMonth(weekday=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2023-07-07 00:00:00')"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Week偏置类型\n",
    "pd.Timestamp(\"20230630\")+pd.offsets.Week()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Timestamp('2023-07-06 00:00:00')"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Timestamp('20230630')+pd.offsets.Week(weekday=3) # 距离6月30号最近的周四"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.3.2.采样频率"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### freq字符串\n",
    "|freq字符串|含义|\n",
    "|:---:|:---:|\n",
    "| B  | 工作日 |\n",
    "| W / W-Mon| 一周 / 周一|\n",
    "| WOM-1Mon | (第一周的周一) |\n",
    "| (B)MS/(B)M | 月初 / 月末  |\n",
    "| (B)QS / (B)Q | 季度初/季度末 |\n",
    "| (B)AS / (B)A | 年初 / 年末 |"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-01', '2021-02-01', '2021-03-01'], dtype='datetime64[ns]', freq='MS')"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"20210101\",'20210331',freq='MS') # 月初"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-31', '2021-02-28', '2021-03-31'], dtype='datetime64[ns]', freq='M')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"20210101\",\"20210331\",freq='M') # 月末"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-01', '2021-01-04', '2021-01-05', '2021-01-06',\n",
       "               '2021-01-07', '2021-01-08'],\n",
       "              dtype='datetime64[ns]', freq='B')"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"20210101\",'20210110',freq='B') # 工作日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-04', '2021-01-11', '2021-01-18', '2021-01-25',\n",
       "               '2021-02-01'],\n",
       "              dtype='datetime64[ns]', freq='W-MON')"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range('20210101',\"20210201\",freq='W-MON') # 周一"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2021-01-04', '2021-02-01'], dtype='datetime64[ns]', freq='WOM-1MON')"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.date_range(\"20210101\",'20210201',freq='WOM-1MON') # 每月第一个周一"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 10.4.时间序列的操作\n",
    "<P>主要介绍三方面的内容一般数据操作中的滑窗,分组和连接操作</P>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 10.4 采样频率滑窗"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01    0\n",
       "2021-01-02    5\n",
       "2021-01-04    8\n",
       "2021-01-05    1\n",
       "2021-01-07    3\n",
       "2021-01-08    0\n",
       "2021-01-09    5\n",
       "2021-01-11    8\n",
       "dtype: int32"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 准备数据\n",
    "import numpy as np\n",
    "np.random.seed(999)\n",
    "s=pd.Series(np.random.randint(0,10,365),index=pd.date_range('20210101','20211231'))\n",
    "s=s.sample(250,random_state=999).sort_index()\n",
    "s.head(8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2021-01-01     0.0\n",
       "2021-01-02     5.0\n",
       "2021-01-04    13.0\n",
       "2021-01-05    14.0\n",
       "2021-01-07    17.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 频率滑窗使用rolling函数\n",
    "s.rolling(\"7D\").sum().head() #每行数据统计前7天的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
